﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Messages;
using Xrm.Framework.Test.Unit.Fakes;
using Microsoft.Xrm.Sdk.Query;
using Xrm.Solutions.FrameworkSample.Plugins;


namespace Xrm.Solutions.FrameworkSample.XrmUnitTests.Plugins
{
    [TestClass]
    public class TestAutoNumberXrmUsingStubs : PluginUnitTest
    {
        #region Instance Variables

        //TODO: Declare your variables for setup and verification
        private string _prefix = "TestPrefix";

        #endregion

        #region Setup

        protected override IPlugin SetupPlugin()
        {
            //TODO: Setup your trigger
            base.SetPluginEvent("account", "Create",
               Xrm.Framework.Test.Unit.SdkMessageProcessingStepImage.PreOperation);

            //TODO: Setup your Stubs & Shim
            base.OrganizationServiceStub.RetrieveMultipleQueryBase = (query) =>
            {
                EntityCollection entities = new EntityCollection();

                if (query is QueryByAttribute)
                {
                    Entity config = new Entity("xdft_configuration");
                    config["xdft_value"] = _prefix;
                    entities.Entities.Add(config);
                }
                return entities;
            };

            base.SetTarget(new Entity("contact"));

            //TODO: Create your Plugin
            IPlugin plugin = new PreAccountCreate();

            return plugin;
        }

        #endregion

        #region Test

        [TestMethod]
        public void RunTestAutoNumberXrmUsingStubs()
        {
            base.Test();
        }

        #endregion

        #region Verify

        protected override void Verify()
        {
            Assert.IsNull(Error);

            Entity modifiedTarget = (Entity)base.GetTarget();

            string reference = modifiedTarget["xdft_reference"].ToString();

            Assert.IsTrue(reference.Contains(_prefix));
        }

        #endregion
    }
}
